import {
  ENTERPRISE_BASIC_INFORMATION_TABLE_ID,
  PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE,
  ENTERPRISE_BASIC_INFORMATION_TABLE_NAME,
  TABLE_ACCOUNT_CONFIGS,

  // REPORT_AUDIT_STATUS_OPTIONS,
  E_UNDER_REPORT,
  E_REPORTED,
  E_RESOLVED,
  E_REJECT,
} from '@/config'

/* IFTRUE_ONLY_NANCHANG
import { getFiveYearPlanInfo } from '@/api/enterpriseQuarterlyReport2';
FITRUE_ONLY_NANCHANG */
/* IFTRUE_NOT_NANCHANG */
import { getFiveYearPlanInfo } from '@/api/enterpriseQuarterlyReport';
/* FITRUE_NOT_NANCHANG */
import { getReportReviewList } from '@/api/reportReview'

export default {
  data () {
    return {
      loading: false,
      lists: [],

      // 五年计划表头字段
      fiveYearsColumns: {
        column1: '',
        column2: '',
        column3: '',
        column4: '',
        column5: '',
      },

      // 表1-4
      small_numeral_name: '',
      first_of_five_year_plan: '',
    }
  },
  methods: {
    async getLists (params) {
      this.loading = true
      try {
        const { data = {} } = await getReportReviewList(params);
        this.lists = data.records || [];
        this.pagination.total = data.total || 0;
      } finally {
        this.loading = false
      }
    },
    getTableName (tableAccountId) {
      // 企业基本情况表
      if (Number(tableAccountId) === ENTERPRISE_BASIC_INFORMATION_TABLE_ID) {
        return ENTERPRISE_BASIC_INFORMATION_TABLE_NAME
      }
      let value, target
      for (const key in TABLE_ACCOUNT_CONFIGS) {
        value = TABLE_ACCOUNT_CONFIGS[key]
        if (value.ID === Number(tableAccountId)) {
          target = value
        }
      }
      if (target) {
        return target.NAME
      }
      return ''
    },
    findTable (id) {
      const tables = Object.keys(TABLE_ACCOUNT_CONFIGS)
      const key = tables.find(key => TABLE_ACCOUNT_CONFIGS[key].ID === Number(id))
      return key ? TABLE_ACCOUNT_CONFIGS[key] : null
    },
    async edit (tableId) {
      const tableAccountId = +tableId;

      // 企业基础情况表
      if (tableAccountId === ENTERPRISE_BASIC_INFORMATION_TABLE_ID) {
        this.tableAccountId = ENTERPRISE_BASIC_INFORMATION_TABLE_ID
        this.tableName = ENTERPRISE_BASIC_INFORMATION_TABLE_NAME
        this.tableColumns = []
        this.tableStatus = ''
        this.readonly = true
        this.$nextTick(() => {
          this.showTableAccount = true
        })
        return
      }

      const TABLE_ACCOUNT = this.findTable(tableAccountId)
      if (TABLE_ACCOUNT === null) {
        return false
      }
      this.tableAccountId = tableAccountId
      this.tableName = TABLE_ACCOUNT.NAME
      this.tableColumns = TABLE_ACCOUNT.COLUMNS
      this.tableStatus = ''
      this.readonly = true

      if (tableAccountId === PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.ID) {
        const five = await getFiveYearPlanInfo(this.year)
          .then(({ data }) => {
            data = data || {}
            const yearList = data.yearList || []
            if (yearList.length > 0) {
              this.small_numeral_name = data.smallNumeralName;
              this.first_of_five_year_plan = yearList[0]

              const a = (y) => `${y}年`;

              this.fiveYearsColumns.column1 = a(yearList[0]);
              this.fiveYearsColumns.column2 = a(yearList[1]);
              this.fiveYearsColumns.column3 = a(yearList[2]);
              this.fiveYearsColumns.column4 = a(yearList[3]);
              this.fiveYearsColumns.column5 = a(yearList[4]);
              return {
                oneYear: a(yearList[0]),
                twoYear: a(yearList[1]),
                threeYear: a(yearList[2]),
                fourYear: a(yearList[3]),
                fiveYear: a(yearList[4]),
              }
            }
            return {
              oneYear: 'xx年',
              twoYear: 'xx年',
              threeYear: 'xx年',
              fourYear: 'xx年',
              fiveYear: 'xx年',
            }
          })

        const cols = [...this.tableColumns]
        Object.keys(five).forEach(key => {
          const target = cols.find(col => col.prop === key)
          if (target) {
            target.label = five[key]
          }
        })

        this.tableColumns = cols
      }

      this.$nextTick(() => {
        this.showTableAccount = true
      })
    },
    stateTagType (state) {
      switch (state) {
        case E_UNDER_REPORT:
          return 'warning'
        case E_REPORTED:
          return 'primary'
        case E_RESOLVED:
          return 'success'
        case E_REJECT:
          return 'danger'
        default:
          return 'info'
      }
    },
    getButtons (state) {
      // 查看 1
      // 审核 2
      // 历史操作 3
      switch (state) {
        case E_UNDER_REPORT:
          return [1]
        case E_REPORTED:
          return [1, 2, 3]
        // 已审核后还可以再次审核
        case E_RESOLVED:
          return [1, 2, 3]
        case E_REJECT:
          return [1, 3]
        default:
          return [1]
      }
    }
  },
}
